#!/bin/bash

IFS_BAK=$IFS
IFS="
"

inv="/Data/Inv"
invT="/Data/Inv/temp"
Movielist="/Data/Inv/temp/Movielist"
Movielist2="/Data/Inv/temp/Movielist2"
Movielist3="/Data/Inv/temp/Movielist3"
finallist="/Data/Inv/Movielist$(/bin/date +%F).csv"
/bin/echo "####################################"
/bin/echo " Gathering Data File from Sources "
/bin/echo "####################################"
/usr/bin/find /MacMedia/Movie/*/*/ -type f -size +126976k > $Movielist
/bin/echo "####################################"
/bin/echo " Generating Details from Sources "
/bin/echo "####################################"
/bin/echo "\"Title\";\"Release Date\";\"Year\";\"Rating\";\"FileExt\";\"Video Format\";\"Resolution Size\";\"Video Codec\";\"Video Bitrate\";\"Video Duration\";\"Audio BitRate\";\"Audio Codec\";\"Audio Chanels\";\"File Size\";\"Studio\";\"Director\";\"Directory\";\"Sub Title\";\"Set Box\";\"Genre\"" > $finallist

for line in `/bin/cat $Movielist` ; do
Mf=`/bin/echo $line | /usr/bin/cut -d "/" -f 2`
Cf=`/bin/echo $line | /usr/bin/cut -d "/" -f 3`
Lf=`/bin/echo $line | /usr/bin/cut -d "/" -f 4`
fn=`/bin/echo $line | /usr/bin/cut -d "/" -f 5`
mn=`/bin/echo $line | /usr/bin/cut -d "/" -f 6`
file="/$Mf/$Cf/$Lf/$fn/$mn"
/bin/echo "############# Parsing for $fn ###############"
/usr/bin/mplayer -vo null -ao null -identify -frames 0 "$file" | /bin/grep "^ID" > $Movielist2
/bin/cat $Movielist2 | /bin/grep "^ID" | /usr/bin/sort -u > $Movielist3 
name=`/bin/echo "$fn" | /usr/bin/rev | /usr/bin/cut -c 8- | /usr/bin/rev`
year=`/bin/echo "$fn" | /usr/bin/rev | /usr/bin/cut -c -6 | /usr/bin/rev`
ext=`/bin/echo "$mn" | /usr/bin/rev | /usr/bin/cut -c -3 | /usr/bin/rev`
vfor=`/bin/cat $Movielist3 | /bin/grep "^ID_VIDEO_FORMAT" | /usr/bin/cut -d "=" -f 2 | /bin/sed -n '$p'`
vh=`/bin/cat $Movielist3 | /bin/grep "^ID_VIDEO_HEIGHT" | /usr/bin/cut -d "=" -f 2 | /bin/sed -n '$p'`
vw=`/bin/cat $Movielist3 | /bin/grep "^ID_VIDEO_WIDTH" | /usr/bin/cut -d "=" -f 2 | /bin/sed -n '$p'`
vcod=`/bin/cat $Movielist3 | /bin/grep "^ID_VIDEO_CODEC" | /usr/bin/cut -d "=" -f 2 | /bin/sed -n '$p'`
vbr=`/bin/cat $Movielist3 | /bin/grep "^ID_VIDEO_BITRATE" | /usr/bin/cut -d "=" -f 2 | /bin/sed -n '$p'`
abr=`/bin/cat $Movielist3 | /bin/grep "^ID_AUDIO_BITRATE" | /usr/bin/cut -d "=" -f 2 | /bin/sed -n '$p'`
ac=`/bin/cat $Movielist3 | /bin/grep "^ID_AUDIO_CODEC" | /usr/bin/cut -d "=" -f 2 | /bin/sed -n '$p'`
ach=`/bin/cat $Movielist3 | /bin/grep "^ID_AUDIO_NCH" | /usr/bin/cut -d "=" -f 2 | /bin/sed -n '$p'`
vl=`/bin/cat $Movielist3 | /bin/grep "^ID_LENGTH" | /usr/bin/cut -d "=" -f 2 | /bin/sed -n '$p'`
sub=`/bin/cat $Movielist3 | /bin/grep "^ID_FILE_SUB_FILENAME" | /usr/bin/cut -d '=' -f 2 | /bin/sed -n '$p'`
vlm=`/bin/echo "$vl/60" | /usr/bin/bc`
nfo=`/bin/echo "$mn" | /bin/sed 's/...$/nfo/'`
nfofile="/$Mf/$Cf/$Lf/$fn/$nfo"
fsz=`/bin/ls -sh --block-size=M "$file" | /usr/bin/cut -d " " -f 1`
rdates=`/usr/bin/xml_grep releasedate --text_only  "$nfofile"`
studio=`/usr/bin/xml_grep studio --text_only  "$nfofile" | /bin/sed ':a;N;$!ba;s/\n/ \& /g'`
dir=`/usr/bin/xml_grep director --text_only  "$nfofile"| /bin/sed ':a;N;$!ba;s/\n/ \& /g'`
rate=`/usr/bin/xml_grep rating --text_only  "$nfofile"`
genre=`/usr/bin/xml_grep genre --text_only  "$nfofile" | /usr/bin/sort -u | /bin/sed ':a;N;$!ba;s/\n/ \| /g'` 
sbox=`/usr/bin/xml_grep set --text_only  "$nfofile"`
/bin/echo "\"$name\";\"$rdates\";\"$year\";\"$rate\";\"$ext\";\"$vfor\";\"$vh x $vw\";\"$vcod\";\"$vbr\";\"$vlm\";\"$abr\";\"$ac\";\"$ach\";\"$fsz\";\"$studio\";\"$dir\";\"$Mf\";;\"$sub\";\"$sbox\";\"$genre\"" >> $finallist
done

/bin/mv $finallist '/Data/Users/mac/Google Drive/ColectionsList/List/'

/bin/echo "DONE Generating - `/bin/date`"
IFS=$IFS_BAK
IFS_BAK=
